package com.example.lottery_system.controller;

import com.example.lottery_system.controller.param.PageParam;
import com.example.lottery_system.controller.param.PrizeParam;
import com.example.lottery_system.controller.result.PrizeRecord;
import com.example.lottery_system.controller.result.PageResult;
import com.example.lottery_system.service.PrizeService;
import com.example.lottery_system.service.dto.PrizeListDto;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@RestController
public class PrizeController {

    Logger logger = LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    PrizeService prizeService;

    @RequestMapping("/prize/pic")
    public String picUpload(MultipartFile file) {
            return prizeService.picUp(file);
    }


    @RequestMapping("/prize/create")
    public Long createPrize(@Valid @RequestPart("param") PrizeParam param ,
                            @RequestPart("picFile") MultipartFile picFile) {
        logger.info("得到的参数为：" + param  +  "获取到的图片名为；" + picFile);


        return prizeService.createPrizeService(param,picFile);
    }

    @RequestMapping("/prize/show-prizes")
    public PageResult<List<PrizeRecord>> showPrizes(PageParam param) {

//        接收来自服务层的返回值
        PageResult<List<PrizeListDto>> prizeListDtoList = prizeService.findPrizes(param);

//        构建返回类型
        PageResult<List<PrizeRecord>> pageResult = new PageResult<>(prizeListDtoList.getTotal(),
                prizeListDtoList.getRecords().stream().map(prizeRecordDto -> new PrizeRecord(
                        prizeRecordDto.getPrizeId(),
                        prizeRecordDto.getPrizeName(),
                        prizeRecordDto.getDescription(),
                        prizeRecordDto.getPrice(),
                        prizeRecordDto.getImageUrl()
                )).collect(Collectors.toList()));

        return pageResult;
    }


    @RequestMapping("/prize/delete")
    public Boolean deletePrize(Long prizeId) {
        logger.info("deletePrize::prizeId:{}",prizeId);
        return prizeService.deletePrize(prizeId);
    }

}
